package com.syj.jsxjj.ui.renzheng.activity;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.syj.jsxjj.R;
import com.syj.jsxjj.app.App;
import com.syj.jsxjj.base.BaseActivity;
import com.syj.jsxjj.events.ChangeTabMainEvent;
import com.syj.jsxjj.http.HttpManager;
import com.syj.jsxjj.http.HttpSubscriber;
import com.syj.jsxjj.ui.main.FragmentFactory;
import com.syj.jsxjj.ui.renzheng.bean.ContentBean;
import com.syj.jsxjj.ui.renzheng.bean.OperatorBean;
import com.syj.jsxjj.ui.renzheng.fragment.AuthenFragment;
import com.syj.jsxjj.util.StringUtil;
import com.syj.jsxjj.util.ToastUtil;
import com.syj.jsxjj.widget.ClearEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;


public class PhoneActivity extends BaseActivity {
    @BindView(R.id.ed_phone)
    ClearEditText editPhone;
    @BindView(R.id.ed_password)
    ClearEditText editPassword;
    private ContentBean contentBean;
    @BindView(R.id.tv_finish_phone)
    TextView mFinishPhoneText;
    @BindView(R.id.ll_phone)
    LinearLayout mPhoneLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_phone;
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void loadData() {
        mTitle.setTitle("手机运营商");
        mTitle.setRightTitle("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        contentBean = getIntent().getParcelableExtra(AuthenFragment.CONTENT_BEAN);
        if (contentBean==null){
            contentBean = new ContentBean();
        }
        if (contentBean.getOperator()!=null){
//            editPhone.setText(contentBean.getOperator().getPhone());
//            editPassword.setText(contentBean.getOperator().getPassword());
            mPhoneLayout.setVisibility(View.GONE);
            mFinishPhoneText.setVisibility(View.VISIBLE);
            mTitle.setRightTitle("去借款", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new ChangeTabMainEvent(FragmentFactory.FragmentStatus.Lend));
                    finish();
                }
            });
        }else {
            mPhoneLayout.setVisibility(View.VISIBLE);
            mFinishPhoneText.setVisibility(View.GONE);
        }
        initLisenters();
    }

    private void save() {
        if (StringUtil.isBlankEdit(editPhone)) {
            ToastUtil.showToast("请输入手机号");
        } else if (StringUtil.isBlankEdit(editPassword)) {
            ToastUtil.showToast("请输入服务密码");
        }else{
            OperatorBean contactBean = new OperatorBean();
            contactBean.setPhone(editPhone.getText().toString());
            contactBean.setPassword(editPassword.getText().toString());
            contentBean.setOperator(contactBean);
            Log.e("contentBean=",new Gson().toJson(contentBean));
            toSubscribe(HttpManager.getApi().saveCertification("1", new Gson().toJson(contentBean)), new HttpSubscriber() {
                @Override
                protected void _onStart() {
                    App.loadingDefault(mActivity);
                }

                @Override
                protected void _onNext(Object o) {
                    ToastUtil.showToast("保存成功");
                    mPhoneLayout.setVisibility(View.GONE);
                    mFinishPhoneText.setVisibility(View.VISIBLE);
                    mTitle.setRightTitle("去借款", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            EventBus.getDefault().post(new ChangeTabMainEvent(FragmentFactory.FragmentStatus.Lend));
                            finish();
                        }
                    });
                }

                @Override
                protected void _onError(String message) {
                    ToastUtil.showToast(message);
                }

                @Override
                protected void _onCompleted() {
                    App.hideLoading();
                }
            });
        }
    }


    private void initLisenters() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
